package com.young.shop_admin.services.impl;

import com.young.shop_admin.dao.GoodsDao;
import com.young.shop_admin.dao.impl.GoodsDaoImpl;
import com.young.shop_admin.pojo.entity.Goods;
import com.young.shop_admin.pojo.entity.Page;
import com.young.shop_admin.services.GoodsService;

import java.sql.SQLException;
import java.util.List;

/**
 *
 */

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsList = new GoodsDaoImpl();

    @Override
    public List<Goods> getAllGoods() {

        return goodsList.getAllGoods();
    }

    @Override
    public Goods checkSoldOutGoods(Integer id) {
        //判断id 是否为空
        if (id == null) {
            throw new RuntimeException("无法查询该商品信息");
        }

        //根据商品id修改商品信息
        int i = goodsList.updateGoodsById(id, "已下架");
        if (i != 1) {
            throw new RuntimeException("商品已下架");
        }
        //根据商品id查询商品信息
        return goodsList.queryGoodsById(id);
    }

    @Override
    public Goods checkShelvesGoods(Integer id) {
        //判断id 是否为空
        if (id == null) {
            throw new RuntimeException("无法查询该商品信息");
        }

        //根据商品id修改商品信息
        int i = goodsList.updateGoodsById(id, "热售中");
        if (i != 1) {
            throw new RuntimeException("商品已下架");
        }
        //根据商品id查询商品信息
        return goodsList.queryGoodsById(id);
    }


    //Service层处理所有商品信息的数据给web层
    @Override
    public List<Goods> findAllGoods() throws SQLException {
        //调用dao层查询所有的商品
        List<Goods> allGoods = goodsList.findAllGoods();
        return allGoods;
    }


    //service层查询某个特定页面的数据给web层
    @Override
    public List<Goods> findGoods(int currentPage, int PageSize) {
        List<Goods> goods = null;
        try {
            goods = goodsList.findGoods(currentPage, PageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return goods;
    }


    @Override
    public Page findPageInfo(int currentPage, int PageSize, String type) throws SQLException {

        //type为空分页查询所有数据，type不为空根据类型查询数据
        List<Goods> goods;
        if (type == null || "".equals(type.trim())) {
            goods = goodsList.findGoods(currentPage, PageSize);

        } else {
            goods = goodsList.getGoodsTypePage(currentPage, PageSize, type);

        }


        int totalCount = goodsList.findTotalCount();

        Page page = new Page();
        //封装数据
        page.setTotalCount(totalCount);
        page.setCurrentPage(currentPage);
        page.setPageSize(PageSize);
        page.setList(goods);

        //向上取整,计算总页数,不要忘了乘以1.0,否则会少一页数据
        int totalPage = (int) Math.ceil(totalCount * 1.0 / PageSize);
        page.setTotalPage(totalPage);

        //把数据给servlet
        return page;


    }

    @Override
    public List<Goods> selectGoodsListByType(String type) {
        //判断type是否为空，为空赋值
        if (type == null || "".equals(type.trim())) {
            return goodsList.getAllGoods();
        }
        List<Goods> goodsByType = goodsList.getGoodsByType(type);
        return goodsByType;

        // return goodsList.getGoodsByType(type);
    }
}
